import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lcapp/api/interface/user_api.dart';
import 'package:lcapp/api/request/response_data.dart';
import 'package:lcapp/basic/user_info.dart';
import 'package:lcapp/config/const_config.dart';
import 'package:lcapp/model/share/custom_share.dart';
import 'package:lcapp/model/toast/toast.dart';
import 'package:lcapp/widget/asr_app_bar.dart';

class MinGetMoneyPage extends StatefulWidget {
  const MinGetMoneyPage({Key? key}) : super(key: key);

  @override
  _MinGetMoneyPageState createState() => _MinGetMoneyPageState();
}

class _MinGetMoneyPageState extends State<MinGetMoneyPage> {
  TextEditingController nameController = TextEditingController();
  TextEditingController cardController = TextEditingController();
  TextEditingController khhController = TextEditingController();
  int selIndex = 0;
  int getType = 0;
  List<Map<String, dynamic>> golds = [
    {'money': 5,},
    {'money': 10},
    {'money': 25},
    {'money': 50},
    {'money': 100},
    {'money': 200}
  ];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    refreshUserInfo(refreshBlock: () {
      if (mounted) setState(() {});
    });
  }

  getMoney()async{
    if(nameController.text.trim().isEmpty||cardController.text.trim().isEmpty){
      showToast("请先输入姓名、账号信息");
      return;
    }
    if(getType==1&&khhController.text.trim().isEmpty){
      showToast("请先输入开户行信息");
      return;
    }
    showLoading();
    Map<String,dynamic> map = {
      'amount':golds[selIndex]['money'],
      'phone':userInfo.phone,
      'realName':nameController.text.trim(),
      'withdrawType':getType
    };
    if(getType==1){
      map['unionpay'] = cardController.text.trim();
      map['openBank'] = khhController.text.trim();
    }else{
      map['alipay'] = cardController.text.trim();
    }
    ResponseData res = await UserAPI.minGetMoney(map);
    hideLoading();
    if(res.code==200){
      showToast("提现申请成功!");
      Navigator.of(context).pop();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AsrAppBar(
        backgroundColor: defaultBarColor,
        barColor: Colors.white,
        elevation: 1,
        titleText: '提现',
        actions: [
          GestureDetector(
            onTap: (){
              Navigator.of(context).pushNamed('MinGetMoneyListPage');
            },
            child: Container(
              alignment: Alignment.center,
              padding: EdgeInsets.only(right: 30.w),
              child: Text('提现记录'),
            ),
          )
        ],
      ),
      body: GestureDetector(
        onTap: (){
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: Container(
          child: SingleChildScrollView(
            child: Column(
              children: [
                Container(
                  padding: EdgeInsets.symmetric(vertical: 50.w),
                  decoration: BoxDecoration(
                    color: defaultBarColor,
                  ),
                  child: Column(
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(
                            '${userInfo.cash ?? 0}',
                            style: TextStyle(
                                color: Colors.white,
                                fontSize: 60.sp,
                                fontWeight: FontWeight.w600),
                          )
                        ],
                      ),
                      SizedBox(
                        height: 20.w,
                      ),
                      Text(
                        '我的收益',
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 28.sp,
                        ),
                      )
                    ],
                  ),
                ),
                Container(
                  margin: EdgeInsets.symmetric(vertical: 40.w),
                  padding: EdgeInsets.symmetric(horizontal: 30.w),
                  child: Row(
                    children: [
                      Container(
                        width: 5,
                        height: 50.w,
                        color: Color(0xffFF7916),
                      ),
                      SizedBox(
                        width: 20.w,
                      ),
                      Text(
                        '提现金额',
                        style: TextStyle(
                            color: Colors.black,
                            fontWeight: FontWeight.w600,
                            fontSize: 40.sp),
                      ),
                    ],
                  ),
                ),
                Container(
                  padding: EdgeInsets.symmetric(horizontal: 30.w),
                  child: GridView.builder(
                      physics: NeverScrollableScrollPhysics(),
                      padding: EdgeInsets.zero,
                      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                          crossAxisCount: 2,
                          mainAxisSpacing: 11.w,
                          crossAxisSpacing: 11.w,
                          childAspectRatio: 3.5),
                      shrinkWrap: true,
                      itemCount: golds.length,
                      itemBuilder: (BuildContext context, int i) {
                        return GestureDetector(
                          onTap: () {
                            FocusScope.of(context).requestFocus(FocusNode());
                            setState(() {
                              selIndex = i;
                            });
                          },
                          child: Container(
                            child: Stack(
                              children: [
                                Image.asset(
                                  selIndex == i
                                      ? 'assets/min/min_hjb.png'
                                      : 'assets/min/min_hjb_un.png',
                                  fit: BoxFit.fill,
                                  width: (1.sw - 60.w - 11.w) * 0.5,
                                  height: (1.sw - 60.w - 11.w) * 0.5 / 3.5,
                                ),
                                Center(
                                  child: Column(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    children: [
                                      Text(
                                        '${golds[i]['money']}元',
                                        style: TextStyle(
                                            color: Color(0xff3D3D3D),
                                            fontWeight: FontWeight.w600,
                                            fontSize: 40.sp),
                                      ),
                                    ],
                                  ),
                                )
                              ],
                            ),
                          ),
                        );
                      }),
                ),
                Container(
                  margin: EdgeInsets.symmetric(vertical: 40.w),
                  padding: EdgeInsets.symmetric(horizontal: 30.w),
                  child: Row(
                    children: [
                      Container(
                        width: 5,
                        height: 50.w,
                        color: Color(0xffFF7916),
                      ),
                      SizedBox(
                        width: 20.w,
                      ),
                      Text(
                        '提现方式',
                        style: TextStyle(
                            color: Colors.black,
                            fontWeight: FontWeight.w600,
                            fontSize: 40.sp),
                      ),
                    ],
                  ),
                ),
                Container(
                  padding: EdgeInsets.symmetric(horizontal: 30.w),
                  child: Row(
                    children: [
                      GestureDetector(
                        onTap: () {
                          FocusScope.of(context).requestFocus(FocusNode());
                          setState(() {
                            getType = 0;
                          });
                        },
                        child: Container(
                          width: (1.sw - 60.w ) * 0.5,
                          height: (1.sw - 60.w ) * 0.5 / 3.5,
                          child: Stack(
                            children: [
                              Image.asset(
                                getType == 0
                                    ? 'assets/min/min_hjb.png'
                                    : 'assets/min/min_hjb_un.png',
                                fit: BoxFit.fill,
                                width: (1.sw - 60.w ) * 0.5,
                                height: (1.sw - 60.w ) * 0.5 / 3.5,
                              ),
                              Center(
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    Image.asset(
                                      'assets/min/ali_pay.png',
                                      width: 35.w,
                                    ),
                                    SizedBox(
                                      width: 20.w,
                                    ),
                                    Text(
                                      '支付宝',
                                      style: TextStyle(
                                          color: Color(0xff3D3D3D),
                                          fontWeight: FontWeight.w600,
                                          fontSize: 40.sp),
                                    ),
                                  ],
                                ),
                              )
                            ],
                          ),
                        ),
                      ),
                      GestureDetector(
                        onTap: (){
                          FocusScope.of(context).requestFocus(FocusNode());
                          setState(() {
                            getType=1;
                          });
                        },
                        child: Container(
                          width: (1.sw - 60.w ) * 0.5,
                          height: (1.sw - 60.w ) * 0.5 / 3.5,
                          child: Stack(
                            children: [
                              Image.asset(
                                getType == 1
                                    ? 'assets/min/min_hjb.png'
                                    : 'assets/min/min_hjb_un.png',
                                fit: BoxFit.fill,
                                width: (1.sw - 60.w ) * 0.5,
                                height: (1.sw - 60.w ) * 0.5 / 3.5,
                              ),
                              Center(
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: [
                                    Image.asset(
                                      'assets/min/min_yhk.png',
                                      width: 45.w,
                                    ),
                                    SizedBox(
                                      width: 20.w,
                                    ),
                                    Text(
                                      '银联',
                                      style: TextStyle(
                                          color: Color(0xff3D3D3D),
                                          fontWeight: FontWeight.w600,
                                          fontSize: 40.sp),
                                    ),
                                  ],
                                ),
                              )
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                Container(
                  margin: EdgeInsets.symmetric(horizontal: 30.w),
                  decoration: BoxDecoration(
                      border: Border(left: BorderSide(width: 1,color: Colors.grey),right: BorderSide(width: 1,color: Colors.grey),bottom: BorderSide(width: 1,color: Colors.grey),)
                  ),
                  child: Column(
                    children: [
                      Container(
                        height: 100.w,
                        // color: Colors.red,
                        child: TextField(
                          controller: nameController,
                          maxLines: 1,
                          maxLength: 30,
                          style: TextStyle(
                              fontSize: 34.sp
                          ),
                          decoration:  InputDecoration(
                            contentPadding: EdgeInsets.symmetric(horizontal: 30.w),
                            hintText: '请输入真实姓名',
                            hintStyle: TextStyle(
                              color: const Color(0xFF7F7F7F),
                              fontSize: 34.sp,
                              fontWeight: FontWeight.w500,
                            ),
                            counterText: "",
                            border: const OutlineInputBorder(
                              borderSide: BorderSide.none,
                            ),
                            fillColor: Colors.transparent,
                            filled: true,
                          ),
                        ),
                      ),
                      Container(
                        height: 0.5,
                        color: Color(0xffE9E8E9),
                      ),
                      Container(
                        height: 100.w,
                        // color: Colors.red,
                        child: TextField(
                          controller: cardController,
                          maxLines: 1,
                          maxLength: 30,
                          style: TextStyle(
                              fontSize: 34.sp
                          ),
                          decoration:  InputDecoration(
                            contentPadding: EdgeInsets.symmetric(horizontal: 30.w),
                            hintText: getType==1? "银行账号":'支付宝账号',
                            hintStyle: TextStyle(
                              color: const Color(0xFF7F7F7F),
                              fontSize: 34.sp,
                              fontWeight: FontWeight.w500,
                            ),
                            counterText: "",
                            border: const OutlineInputBorder(
                              borderSide: BorderSide.none,
                            ),
                            fillColor: Colors.transparent,
                            filled: true,
                          ),
                        ),
                      ),
                      Container(
                        height: 0.5,
                        color: Color(0xffE9E8E9),
                      ),
                      Visibility(
                          child: Container(
                            height: 100.w,
                            // color: Colors.red,
                            child: TextField(
                              controller: khhController,
                              maxLines: 1,
                              style: TextStyle(
                                  fontSize: 34.sp
                              ),
                              decoration:  InputDecoration(
                                contentPadding: EdgeInsets.symmetric(horizontal: 30.w),
                                hintText: "开户银行",
                                hintStyle: TextStyle(
                                  color: const Color(0xFF7F7F7F),
                                  fontSize: 34.sp,
                                  fontWeight: FontWeight.w500,
                                ),
                                counterText: "",
                                border: const OutlineInputBorder(
                                  borderSide: BorderSide.none,
                                ),
                                fillColor: Colors.transparent,
                                filled: true,
                              ),
                            ),
                          ),
                        visible: getType==1,
                      )
                    ],
                  ),
                ),
                GestureDetector(
                  onTap: (){
                    FocusScope.of(context).requestFocus(FocusNode());
                    getMoney();
                  },
                  child: Container(
                    margin: EdgeInsets.symmetric(vertical: 40.w),
                    padding: EdgeInsets.symmetric(horizontal: 30.w),
                    child: Container(
                      height: 80.w,
                      width: 1.sw - 60.w,
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                          color: Color(0xffFA6E28),
                          borderRadius: BorderRadius.circular(80.w)),
                      child: Text(
                        '确认提现',
                        style: TextStyle(
                            color: Colors.white,
                            fontWeight: FontWeight.w600,
                            fontSize: 34.sp),
                      ),
                    ),
                  ),
                ),
                Container(
                  child: RichText(
                    maxLines: 1,
                    softWrap: true,
                    text: TextSpan(
                      text: '遇到问题？',
                      style: TextStyle(
                        fontSize: 26.w,
                        color: const Color(0xff999999),
                      ),
                      children: <TextSpan>[
                        TextSpan(
                          text: '联系客服',
                          style:  TextStyle(
                            color: const Color(0xff224D79),
                            fontSize: 26.w,
                          ),
                          recognizer: TapGestureRecognizer()
                            ..onTap = () {
                              // CustomShare().WXChatService();
                            },
                        ),
                      ],
                    ),
                    textAlign: TextAlign.left,
                  ),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}
